{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8a1d790",
   "metadata": {},
   "source": [
    "## Enumerate"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96eb867a",
   "metadata": {},
   "source": [
    "- Adds number to the list\n",
    "- Counter\n",
    "- Used when you need counter or positions or Indexes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae38968f",
   "metadata": {},
   "source": [
    "### Syntax"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1e467d9",
   "metadata": {},
   "source": [
    "- enumerate(list)\n",
    "    - Takes list\n",
    "    - Returns tupple by default (iterable so has to be converted explicitly)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f7f9fa5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "list1=['a','b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f6519f05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<enumerate at 0x2622655c500>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "enumerate(list1) # returns iterable obj <> which is tuple by default but can be converted to list as well"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8755a5a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 'a'), (1, 'b')]\n",
      "[(0, 'a'), (1, 'b')]\n",
      "[[0, 'a'], [1, 'b']]\n"
     ]
    }
   ],
   "source": [
    "print([i for i in enumerate(list1)])  # by default returns tupple we just returned i\n",
    "\n",
    "print([(i,j) for i,j in enumerate(list1)]) # this is same thing as above\n",
    "\n",
    "#print([i,j for i,j in enumerate(list1)])   # this will give error because you are explicitly returning two vars\n",
    "\n",
    "print([[i,j] for i,j in enumerate(list1)]) # can be converted to list as well"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "774917ba",
   "metadata": {},
   "source": [
    "## Use case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3583f574",
   "metadata": {},
   "outputs": [],
   "source": [
    "#All the if conditions can be applied since it is list comprehension"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "927866ae",
   "metadata": {},
   "source": [
    "### Getting even indexed values\n",
    "- Note:It is based on index not actual values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cf94dc44",
   "metadata": {},
   "outputs": [],
   "source": [
    "list=[11,13,12,14,15,76,89]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f598e7e",
   "metadata": {},
   "source": [
    "### Get the index only if index==even"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c2b44165",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 11), (2, 12), (4, 15), (6, 89)]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[(i,j) for i,j in enumerate(list) if i%2==0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2298e6a4",
   "metadata": {},
   "source": [
    "### Get the index only if value= 76"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8f4380bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i,j in enumerate(list) if j==76]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}